package com.zzxx.wechat.controller;


import com.zzxx.wechat.dao.ArcteryxSkuDao;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller

public class ArcteryxGiftController {

    @Autowired
    ArcteryxSkuDao arcteryxSkuDao;

    public static void main(String[] args) {
        // 设置ChromeDriver的路径
        // System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        System.setProperty("webdriver.chrome.driver", "D:\\software\\chromedriver\\chromedriver-win64\\chromedriver.exe");



        ChromeOptions options = new ChromeOptions();
        //  options.addArguments("---headless");


        //  options.addArguments("--proxy-server=http://brd.superproxy.io:33335");
        //  options.addArguments("--proxy-auth=brd-customer-hl_b3246fe5-zone-residential_proxy1-country-jp:n62p45lthsqb");


        // 创建Chrome浏览器的实例
        WebDriver driver = new ChromeDriver(options);

        // 打开网页
        driver.get("https://wbiprod.storedvalue.com/wbir/clients/arcteryx?lng=en");

        // 等待页面加载完成
        WebDriverWait wait = new WebDriverWait(driver, 60); // 最多等待60秒

        try {
            // 等待直到可以找到cardNo元素
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cardNo")));

            // 获取动态加载后的完整页面HTML
            String dynamicPageSource = (String) ((JavascriptExecutor) driver).executeScript("return document.documentElement.outerHTML;");
            System.out.println("动态加载后的页面HTML:");
            System.out.println(dynamicPageSource);

            // 选择ID为cardNo的输入框并输入内容122
            WebElement cardNoInput = driver.findElement(By.id("cardNo"));
            cardNoInput.sendKeys("6006496947757759315");


            // 选择ID为cardNo的输入框并输入内容122
            WebElement cardPinInput = driver.findElement(By.id("pin"));
            cardPinInput.sendKeys("9583");
            System.out.println("成功找到输入框并输入内容");
            System.out.println("成功找到输入框并输入内容");

            // 根据类选择器找到按钮并点击
            WebElement button = driver.findElement(By.className("arcteryxButton"));
            button.click();
            System.out.println("成功点击按钮");

            // 等待并读取类选择器card-bal-val的内容
            wait.until(ExpectedConditions.presenceOfElementLocated(By.className("card-bal-val")));
            WebElement balanceElement = driver.findElement(By.className("card-bal-val"));
            String balanceText = balanceElement.getText();
            System.out.println("获取到的余额信息: " + balanceText);
        } catch (TimeoutException e) {
            System.out.println("超时：无法找到ID为'cardNo'的元素或页面加载超时");

            // 即使找不到元素，也尝试获取当前页面源码
            String currentPageSource = driver.getPageSource();
            System.out.println("当前页面HTML源码:");
            System.out.println(currentPageSource);
        } catch (NoSuchElementException e) {
            System.out.println("无法找到指定的按钮元素或card-bal-val元素");
        }

        // 关闭浏览器
        //    driver.quit();
    }

}
